﻿' Copyright (c) 2010 Skobkin.ru
' Permission is hereby granted, free of charge, to any person obtaining a copy of
'this software and associated documentation files (the "Software"), to deal in the
' Software without restriction, including without limitation the rights to use, copy,
' modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
' and to permit persons to whom the Software is furnished to do so, subject to the
' following conditions:
' The above copyright notice and this permission notice shall be included in all
' copies or substantial portions of the Software.
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
' INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
' PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
' HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
' OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
' SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

' Hi, Juick :)

Imports System.Console
Module Module1
    Public Logging As Boolean = False
    Sub Main()
        Dim UserName As String
        ' Проверяем, есть ли аргумент
        If Environment.GetCommandLineArgs.Length > 1 Then
            ' Читаем аргументы

            For i As Short = 1 To Environment.GetCommandLineArgs.Length - 1
                Select Case Environment.GetCommandLineArgs(i).ToLower
                    ' Получаем параметры
                    Case "--log"
                        Logging = True
                        ' Другие опции потом
                    Case "--help"
                        ' Показываем справку и выходим
                        WriteLine("Usage:" & vbCrLf & "<juickdiff> [username] [--log] [--help]")
                        End
                    Case Else
                        If Not (Environment.GetCommandLineArgs(i).StartsWith("-")) Then UserName = Environment.GetCommandLineArgs(i).ToLower
                End Select
            Next

            ' Убираем собаку
            If UserName.StartsWith("@") Then UserName = UserName.Remove(0, 1)
        Else
            WriteLine("Username not specified. Please, enter Juick username")
            UserName = Console.ReadLine().ToLower

            ' Спрашиваем о логе
            Dim tmp
            While Not (tmp = "y" Or tmp = "n")
                WriteLine("Do you want to log changes?")
                tmp = Console.ReadKey.KeyChar.ToString.ToLower
                WriteLine()
            End While
            If tmp = "y" Then Logging = True

        End If

        If UserName = Nothing Then
            WriteLine("No Username specified!")
            End
        End If
        ' Получаем HTML
        If Environment.GetCommandLineArgs.Length <= 1 Then _
        Console.WriteLine("Reading http://juick.com/" & UserName & "/readers")

        ' Запрашиваем HTML с подписчиками
        Dim HTML As String = GetResponse("http://juick.com/" & UserName & "/readers")
        Select Case HTML
            Case "404"
                WriteLine("404. Not found :(")
                ReadKey()
                End
            Case Nothing
                WriteLine("HTML code is nothing :(")
                ReadKey()
                End
        End Select

        ' Получаем список подписчиков в массив
        Dim Subscribers() As String = ParseFriends(HTML)

        If IO.File.Exists(UserName & ".txt") Then
            ' Файл есть. Сравниваем
            Diff(Subscribers, UserName & ".txt")
        Else
            ' Файла нет. Заполняем.
            WriteLine("It looks like first run... Indexing subscribers...")
        End If

        Using SW As IO.StreamWriter = New IO.StreamWriter(UserName & ".txt")
            For i As Long = 0 To Subscribers.Length - 2
                ' Записываем свежих подписчиков в файл.
                SW.WriteLine(Subscribers(i))
            Next
            SW.Close()
        End Using

        ' Если запуск с аргументом - закрывать сразу
        If Environment.GetCommandLineArgs.Length <= 1 Then ReadKey()
    End Sub

End Module
